/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.reciever.fileMover;

import dom.Reciever;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author julian.pena
 * @author rodrigo.estrada
 * 
 */
public class FileMover implements Reciever{
    
    File srcDir;
    File dstDir;
    
    public FileMover(String srcDir, String dstDir){
      
        this.srcDir = new File(srcDir);
        if(this.srcDir.isFile()){
            String[] var=  srcDir.split("/");     
            dstDir+="/"+var[var.length-1];            
        }
        this.dstDir = new File(dstDir);
    }
    
    @Override
    public InputStream getInputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setInputStream(InputStream inputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public OutputStream getOutputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setOutputStream(OutputStream outputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void action() throws IOException {
        // throw new UnsupportedOperationException("Not supported yet.");
        try {
            copyDirectory(srcDir, dstDir);
        } catch (IOException ex) {
            Logger.getLogger(FileMover.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
    }
    
    public void copyDirectory(File srcDir, File dstDir) throws IOException { 
        if (srcDir.isDirectory()) { 
            if (!dstDir.exists()) { 
                dstDir.mkdirs(); 
            } 
             
            String[] children = srcDir.list(); 
            for (int i=0; i<children.length; i++) { 
                copyDirectory(new File(srcDir, children[i]), 
                    new File(dstDir, children[i])); 
                new File(srcDir, children[i]).delete();
            } 
        } else { 
            copy(srcDir, dstDir); 
            srcDir.delete();
        } 
    }
       
       
    public void copy(File src, File dst) throws IOException { 
        InputStream in = new FileInputStream(src); 
        OutputStream out = new FileOutputStream(dst); 
         
         
        byte[] buf = new byte[1024]; 
        int len; 
        while ((len = in.read(buf)) > 0) { 
            out.write(buf, 0, len); 
        } 
        in.close(); 
        out.close(); 
    } 
   
}
